#include <stdio.h>
#include <stdlib.h>
#include "linklist.h"
void linklist_insert_node(link_list **head,int value,int index)
{
    link_list *insert_node = (link_list *)malloc(sizeof(link_list));

    insert_node->value = value;
    insert_node->next = NULL;
    if(index == 1)
    {
        insert_node->next = *head;
        *head = insert_node;
        return ;
    }
    else if(index >=2)
    {
        link_list *temp_node = *head;
        for(int i = 0;i < index - 2 && temp_node != NULL; i++)
        {   
            if(temp_node->next != NULL)
                temp_node = temp_node->next;
        }
        insert_node->next = temp_node->next;
        temp_node->next = insert_node;
    }
}
void linklist_print_node(link_list *head)
{
    link_list *temp_node = head;
    while(temp_node!=NULL)
    {
        printf("linklsit node is %d\n",temp_node->value);
        temp_node = temp_node->next;
    }

}
void linklist_reverse_node(link_list **head)
{
    link_list *current,*next,*prev,*temp;
    current = *head;
    prev = NULL;
    while(current!=NULL)
    {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    *head = prev;

}
void linklist_reverse_recursive(link_list **node)
{
    if(*node == NULL || (*node)->next == NULL)
        return ;
    link_list *current = *node;
    link_list *next_node = current->next;
    linklist_reverse_recursive(&next_node);
    current->next->next = current;
    current->next = NULL;
    *node = next_node;
}

void linklist_delete_node(link_list **head,int index)
{
    link_list *temp = *head;
    if(temp != NULL)
    {
        if(index == 1)
        {
            *head = temp->next;
            free(temp);
            return;
        }
        for(int i = 0;i < index - 2;i++)
            temp = temp->next;
        link_list *temp1 = temp->next;
        temp->next = temp1->next;
        free(temp1);
    }
}
int main ()
{
    link_list *head = NULL;
    linklist_insert_node(&head,1,1);
    linklist_insert_node(&head,10,2);
    linklist_insert_node(&head,30,1);
    linklist_insert_node(&head,40,1);
    linklist_insert_node(&head,50,3);
    linklist_print_node(head);
    linklist_reverse_recursive(&head);
    linklist_print_node(head);
}